package com.sky.service.user.impl;

import com.alibaba.fastjson.JSON;
import com.hxc.pay.wx.service.PaySuccessService;
import com.sky.mapper.user.OrderMapper;
import com.sky.pojo.Orders;
import com.sky.websocket.WebSocketServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

@Service
public class PaySuccessServiceImpl implements PaySuccessService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private WebSocketServer webSocketServer;
    @Override
    public void success(String outTradeNo) {

        //修改订单状态，付款时间，付款状态即可
        Orders orders = Orders.builder()
                .number(outTradeNo)
                .status(Orders.TO_BE_CONFIRMED)
                .checkoutTime(LocalDateTime.now())
                .payStatus(Orders.PAID)
                .build();
        orderMapper.updateOrderPaySuccess(orders);

        //通过websocket提醒客户端 来新订单了
        //构建消息
        Map<String,Object> map = new HashMap<>();
        map.put("type",1);
        map.put("orderId",orders.getNumber());
        map.put("content","您有新订单,请及时处理");
        //发送消息
        webSocketServer.sendAll(JSON.toJSONString(map));
    }
}
